﻿using System;
using System.IO;
using System.Linq;
using Ayatta.Contract.Domain;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace Ayatta.Contract.Extension
{
    public static class ProductExtension
    {
        public static IDictionary<string, string> Props(this ProductItem item)
        {
            var dic = new Dictionary<string, string>();

            var names = item.PropStr.Split(';');
            var list = names.Select(o => new { Pname = o.Split(':')[2].Trim(), Vname = o.Split(':')[3].Trim() });
            foreach (var o in list.GroupBy(o => o.Pname))
            {
                var key = o.Key;
                var value = string.Join(",", o.Select(x => x.Vname).ToArray());
                dic.Add(key, value);
            }
            return dic;
        }

        public static IDictionary<string, IDictionary<string, string>> SaleProps(this IList<ProductItemSku> skus)
        {
            var dic = new Dictionary<string, IDictionary<string, string>>();
            if (skus != null && skus.Count > 0)
            {
                foreach (var sku in skus)
                {
                    var names = sku.PropStr.Split(';');
                    var list = names.Select(o => new { Pid = o.Split(':')[0].Trim(), Vid = o.Split(':')[1].Trim(), Pname = o.Split(':')[2].Trim(), Vname = o.Split(':')[3].Trim() });
                    foreach (var o in list.GroupBy(o => o.Pname))
                    {
                        var key = o.Key;
                        var pid = o.First(x => x.Pname == o.Key).Pid;
                        var temp = o.ToDictionary(k => (pid + ":" + k.Vid), v => v.Vname);
                        if (dic.ContainsKey(key))
                        {
                            foreach (var t in from t in temp let x = dic[key] where !x.ContainsKey(t.Key) select t)
                            {
                                dic[key].Add(t.Key, t.Value);
                            }
                        }
                        else
                        {
                            dic[key] = temp;
                        }
                    }
                }
            }
            return dic;
        }

        public static string ImageSize(this ProductItemImageSize size)
        {
            var s = string.Empty;
            switch (size)
            {
                case ProductItemImageSize.XXS:
                    s = ".xxs"; break;
                case ProductItemImageSize.XS:
                    s = ".xs"; break;
                case ProductItemImageSize.S:
                    s = ".s"; break;
                case ProductItemImageSize.M:
                    s = ".m"; break;
                case ProductItemImageSize.L:
                    s = ".l"; break;
                case ProductItemImageSize.XL:
                    s = ".xl"; break;
            }
            return s;
        }

        public static string Picture(this ProductItem item, ProductItemImageSize size = ProductItemImageSize.L)
        {
            if (!string.IsNullOrEmpty(item.Picture))
            {
                if (size == ProductItemImageSize.XXS)
                {
                    return item.Picture.Contains(".xl") ? item.Picture.Replace(".xl", ".xxs") : item.Picture.Replace(".l", ".xxs");
                }
                if (size == ProductItemImageSize.XS)
                {
                    return item.Picture.Contains(".xl") ? item.Picture.Replace(".xl", ".xs") : item.Picture.Replace(".l", ".xs");
                }
                if (size == ProductItemImageSize.S)
                {
                    return item.Picture.Contains(".xl") ? item.Picture.Replace(".xl", ".s") : item.Picture.Replace(".l", ".s");
                }
                if (size == ProductItemImageSize.M)
                {
                    return item.Picture.Contains(".xl") ? item.Picture.Replace(".xl", ".m") : item.Picture.Replace(".l", ".m");
                }
                if (size == ProductItemImageSize.L)
                {
                    return item.Picture.Contains(".xl") ? item.Picture.Replace(".xl", ".l") : item.Picture;
                }
                if (size == ProductItemImageSize.XL)
                {
                    return item.Picture.Contains(".xl") ? item.Picture : string.Empty;
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 商品SKU属性图片
        /// First为Pid:Vid Second为L尺寸 Third为S尺寸 Fourth为XXS
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static IList<Magic<string, string, string, string>> PropImgs(this ProductItem item)
        {
            var list = new List<Magic<string, string, string, string>>();

            if (!string.IsNullOrEmpty(item.PropImg))
            {
                var serializer = new JavaScriptSerializer();
                var dic = serializer.Deserialize<Dictionary<string, string>>(item.PropImg);
                foreach (var o in dic)
                {
                    var s = o.Value.Replace(".l", ".s");
                    var xxs = o.Value.Replace(".l", ".xxs");
                    var magic = new Magic<string, string, string, string>(o.Key, o.Value, s, xxs);

                    list.Add(magic);
                }
            }
            return list;
        }

        public static IList<Magic<bool, string, string, string>> ItemImgs(this ProductItem item)
        {
            var list = new List<Magic<bool, string, string, string>>();

            if (!string.IsNullOrEmpty(item.PropImg))
            {
                var serializer = new JavaScriptSerializer();
                var dic = serializer.Deserialize<Dictionary<string, string>>(item.ItemImg);
                foreach (var o in dic)
                {
                    var zoom = o.Value.Contains(".xl");
                    if (zoom)
                    {
                        var xl = o.Value;
                        var l = o.Value.Replace(".xl", ".l");
                        var xs = o.Value.Replace(".xl", ".xs");
                        var magic = new Magic<bool, string, string, string>(true, xl, l, xs);
                        list.Add(magic);
                    }
                    else
                    {
                        var xl = string.Empty;
                        var l = o.Value;
                        var xs = o.Value.Replace(".l", ".xs");
                        var magic = new Magic<bool, string, string, string>(false, xl, l, xs);
                        list.Add(magic);
                    }

                }
            }
            return list;
        }

        public static string Picture(this ProductAuction target, ProductItemImageSize size = ProductItemImageSize.L)
        {
            if (!string.IsNullOrEmpty(target.Picture))
            {
                if (size == ProductItemImageSize.XXS)
                {
                    return target.Picture.Contains(".xl") ? target.Picture.Replace(".xl", ".xxs") : target.Picture.Replace(".l", ".xxs");
                }
                if (size == ProductItemImageSize.XS)
                {
                    return target.Picture.Contains(".xl") ? target.Picture.Replace(".xl", ".xs") : target.Picture.Replace(".l", ".xs");
                }
                if (size == ProductItemImageSize.S)
                {
                    return target.Picture.Contains(".xl") ? target.Picture.Replace(".xl", ".s") : target.Picture.Replace(".l", ".s");
                }
                if (size == ProductItemImageSize.M)
                {
                    return target.Picture.Contains(".xl") ? target.Picture.Replace(".xl", ".m") : target.Picture.Replace(".l", ".m");
                }
                if (size == ProductItemImageSize.L)
                {
                    return target.Picture.Contains(".xl") ? target.Picture.Replace(".xl", ".l") : target.Picture;
                }
                if (size == ProductItemImageSize.XL)
                {
                    return target.Picture.Contains(".xl") ? target.Picture : string.Empty;
                }
            }
            return string.Empty;
        }

        public static string AuctionDiffBuyLimitText(this ProductAuction auction)
        {
            var s = string.Empty;
            if (auction.DiffBuy)
            {
                s = "该拍品在成交后的七天内，";
                switch (auction.DiffBuyLimit)
                {
                    case DiffBuyLimit.Unlimited:
                        s += "可任意补差购买！"; break;
                    case DiffBuyLimit.NTimesOnly:
                        s += "可补差购买数限制在" + auction.DiffBuyLimitValue + "（含）件以内！"; break;
                    case DiffBuyLimit.NTimesPerCustomer:
                        s += "每个用户可补差购买" + auction.DiffBuyLimitValue + "件！"; break;
                }
            }
            return s;
        }

        public static string AuctionFreeBidLimitText(this ProductAuction auction)
        {
            var s = string.Empty;
            if (auction.FreeBid)
            {
                switch (auction.FreeBidLimit)
                {
                    case FreeBidLimit.Unlimited:
                        s = "该拍品可使用任意多免费金币出价！"; break;
                    case FreeBidLimit.NTimesOnly:
                        s = "该拍品最多可接受免费金币出价" + auction.FreeBidLimitValue + "次！"; break;
                    case FreeBidLimit.NTimesPerCustomer:
                        s = "该拍品每个用户最多可使用免费金币出价" + auction.FreeBidLimitValue + "次！"; break;
                }
            }
            return s;
        }

    }
}